import os
import sys
import time
import argparse
sys.path.append(os.getcwd())

import svgwrite
import json
from PIL import Image

from utils.font_size import *
from utils.image import *
from utils.platform import *
from utils.io import *
from components.text import *
from components.rect import *
from components.factory import *
from settings import *

assert is_windows() or is_darwin()

safe_area_size = 20 * SCALE
min_horizontal_space = 50 * SCALE
vertical_space = 20 * SCALE
text_size = 40 * SCALE
icon_height = 100 * SCALE
min_width = 300 * SCALE

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data', dest='Data', type=str, default='./example/sa_pa_distance.json', help='Path of json data file.')
args = parser.parse_args()
data_file_path = args.Data

data_file_name = 'sa_pa_distance_{}'.format(time.strftime('%Y%m%d%H%M%S'))
svg_file_path = './output/{}.svg'.format(data_file_name)
png_file_path = './output/{}.png'.format(data_file_name)
# load data
with open(data_file_path, 'r', encoding='utf8') as file:
    data = json.load(file)

icons = data.get('icons')
icon_imgs = []
for icon in icons:
    icon_file_path = './resources/{}.png'.format(icon)
    icon_img = Image.open(icon_file_path)
    icon_img = icon_img.resize((int(icon_height * (icon_img.width / icon_img.height)), int(icon_height)))
    icon_imgs.append(icon_img)

line_heights = []
loc_lines = []
dis_lines = []

content_width = min_width
lines = data.get('lines')
for line in lines:
    dis_ele = TextAComponent(line.get('distance'), COLOR_WHITE, text_size)
    item_type = line.get('type')
    item_info = line.get('info')

    factory = ComponentFactory(text_size, COLOR_WHITE)
    ele = factory.component(item_type, item_info)
    
    loc_lines.append(ele)
    dis_lines.append(dis_ele)
    line_heights.append(max(ele.height(), dis_ele.height()))
    content_width = max(content_width, ele.width() + min_horizontal_space + dis_ele.width() + 2 * safe_area_size, min_width, max(sum([img.width for img in icon_imgs]), ele.width() + text_size) + 2 * safe_area_size)

content_height = icon_height + sum(line_heights) + (len(line_heights) - 1) * vertical_space + 2 * safe_area_size

dwg = svgwrite.Drawing(svg_file_path, profile='tiny', width=content_width, height=content_height)

bg_rect = DoubleStrokeRectComponent(width=content_width, height=content_height, main_color=COLOR_GREEN, stroke_color=COLOR_WHITE, stroke_width=4 * SCALE, r=16 * SCALE)
bg_rect.draw(dwg, 0, 0)
dwg.add(dwg.rect(
    insert=(4 * SCALE, 4 * SCALE), 
    size=(content_width - 2 * 4 * SCALE, icon_height + 4 * SCALE), 
    rx=16 * SCALE - 4 * SCALE, 
    ry=16 * SCALE - 4 * SCALE, 
    fill=COLOR_WHITE
))
dwg.add(dwg.rect(
    insert=(4 * SCALE, 4 * SCALE + icon_height + 4 * SCALE - (16 * SCALE - 4 * SCALE)), 
    size=(content_width - 2 * 4 * SCALE, 16 * SCALE - 4 * SCALE), 
    rx=0, 
    ry=0, 
    fill=COLOR_WHITE
))

current_line_y = safe_area_size + icon_height
for (loc, dis, height) in zip(loc_lines, dis_lines, line_heights):
    unit_ele = TextAComponent('km', COLOR_WHITE, text_size * 0.6)

    loc_x = safe_area_size
    loc_y = current_line_y + (height - loc.height()) / 2

    unit_x = content_width - safe_area_size - unit_ele.width()
    dis_x = unit_x - dis.width()
    dis_y = current_line_y + (height - dis.height()) / 2
    unit_y = dis_y + (dis.height() - unit_ele.height()) * 0.8
    
    loc.draw(dwg, loc_x, loc_y)
    dis.draw(dwg, dis_x, dis_y)
    unit_ele.draw(dwg, unit_x, unit_y)
    
    current_line_y += (height + vertical_space)

svg2png(dwg, content_width, content_height, svg_file_path, png_file_path)

dst = Image.open(png_file_path)
icon_y = int(2 * 4 * SCALE)
icon_x = int((content_width - sum([img.width for img in icon_imgs])) / 2)
for icon_img in icon_imgs:
    dst.paste(icon_img, (icon_x, icon_y), icon_img)
    icon_x += icon_img.width

save_image(dst, png_file_path)